package com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.service.impl;

import com.css.fxfzxczh.common.utils.UpdateUtil;
import com.css.fxfzxczh.constants.XczhConstants;
import com.css.fxfzxczh.modules.disasterSituationDataManage.houMonData.service.HouMonDataService;
import com.css.fxfzxczh.modules.disasterSituationDataManage.samPoiData.entity.SamPoiData;
import com.css.fxfzxczh.modules.disasterSituationDataManage.samPoiData.service.SamPoiDataService;
import com.css.fxfzxczh.modules.disasterSituationDataManage.samPoiHouSur.entity.HouDamSta;
import com.css.fxfzxczh.modules.disasterSituationDataManage.samPoiHouSur.entity.SamPoiHouSur;
import com.css.fxfzxczh.modules.disasterSituationDataManage.samPoiHouSur.service.SamPoiHouSurService;
import com.css.fxfzxczh.modules.eqDisasterLossAssess.houseEconomicLossAssess.entity.HouseFailureRatioEntity;
import com.css.fxfzxczh.modules.eqDisasterLossAssess.houseEconomicLossAssess.repository.HouseEconomicLossAssessRepository;
import com.css.fxfzxczh.modules.eqDisasterLossAssess.houseEconomicLossAssess.service.HouseEconomicLossAssessService;
import com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.entity.*;
import com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.repository.*;
import com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.repository.impl.CorrectionFactorRepositoryImpl;
import com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.repository.impl.HouseFinishDamageLossRatioRepositoryImpl;
import com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.repository.impl.HouseFinishEconomicLossRepositoryImpl;
import com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.service.HouseFinishLossAssessService;
import com.css.fxfzxczh.modules.fieldWorkManage.surTaskManage.entity.SurTaskManage;
import com.css.fxfzxczh.modules.fieldWorkManage.surTaskManage.service.SurTaskManageService;
import com.css.fxfzxczh.modules.lossAssessment.houseCategoryArea.entity.HouseCategoryArea;
import com.css.fxfzxczh.modules.lossAssessment.houseCategoryArea.repository.HouseCategoryAreaRepository;
import com.css.fxfzxczh.modules.lossAssessment.resetPrice.entity.ResetPrice;
import com.css.fxfzxczh.modules.lossAssessment.resetPrice.repository.ResetPriceRepository;
import com.css.fxfzxczh.modules.lossAssessment.resetPrice.service.ResetPriceService;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.entity.EarEveManage;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.repository.EarEveManageRepository;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.service.EarEveManageService;
import com.css.fxfzxczh.util.PlatformDateUtils;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import com.css.fxfzxczh.util.PlatformSessionUtils;
import com.css.fxfzxczh.util.UUIDGenerator;
import com.css.fxfzxczh.web.PlatformSessionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: fxfzxczh
 * @Author: lhl
 * @CreateDate: 2023/4/24 9:47
 */
@Service
public class HouseFinishLossAssessServiceImpl implements HouseFinishLossAssessService {
    @Resource
    HouseFinishLossAssessRepository houseFinishLossAssessRepository;
    @Resource
    EarEveManageService earEveManageService;
    @Resource
    SurTaskManageService surTaskManageService;
    @Resource
    SamPoiDataService samPoiDataService;
    @Resource
    SamPoiHouSurService samPoiHouSurService;
    @Resource
    HouMonDataService houMonDataService;
    @Resource
    ResetPriceService resetPriceService;
    @Resource
    MiddleHighRenovationRatioRepository middleHighRenovationRatioRepository;
    @Resource
    HouseFinishDamageLossRatioRepository houseFinishDamageLossRatioRepository;
    @Resource
    HouseEconomicLossAssessService houseEconomicLossAssessService;
    @Resource
    HouseCategoryAreaRepository houseCategoryAreaRepository;
    @Autowired
    EarEveManageRepository earEveManageRepository;
    @Autowired
    ResetPriceRepository resetPriceRepository;
    @Autowired
    HouseEconomicLossAssessRepository houseEconomicLossAssessRepository;
    @Autowired
    HouseFinishEconomicLossRepository houseFinishEconomicLossRepository;

    @Autowired
    CorrectionFactorRepository correctionFactorRepository;

    @Override
    public String mhFinishHouseScaleReminder(String ratioType) {
        StringBuilder message = new StringBuilder("");
        EarEveManage currentEvent = earEveManageRepository.getCurrentEvent(PlatformSessionContext.getUserID());
        String areaRatioType = houseFinishLossAssessRepository.getRatioTypeByXsemId(currentEvent.getXsemId());
        if (!ratioType.equals(areaRatioType)) {
            String areaScale = getChineseCharacter(areaRatioType);
            String scale = getChineseCharacter(ratioType);
            message.append("【中高档房屋装修总面积计算】模块选择的城市规模是" + areaScale + "," +
                    "本次选择的城市规模是" + scale + ",城市规模是否继续选择" + scale + "?");
        }
        return message.toString();
    }

    private String getChineseCharacter(String ratioType) {
        if (XczhConstants.BIG_CITY.equals(ratioType)) {
            return "大城市";
        } else if (XczhConstants.MEDIUM_CITY.equals(ratioType)) {
            return "中等城市";
        } else if (XczhConstants.SMALL_CITY.equals(ratioType)) {
            return "小城市";
        }
        return "";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void mhFinishHouseCount(Map<String, Object> map) {
        List<MhFinishHouseArea> list = new ArrayList<>();
        String xsemId = (String) map.get("xsemId");
        String ratioType = (String) map.get("ratioType");
        String concreteValue = (String) map.get("concreteValue");
        String masonryValue = (String) map.get("masonryValue");
        //获取当前地震事件
        EarEveManage earEveManage = earEveManageRepository.findById(xsemId);

        houseFinishLossAssessRepository.delMhFinishHouseAreaByXsemId(xsemId);
        //获取当前地震事件下数据齐全的评估子区
        List<SurTaskManage> surTaskManageList = houseEconomicLossAssessService.getEvaluationSubArea(xsemId);
        //钢筋混凝土房屋占比
        BigDecimal concreteHouseRatio = BigDecimal.ZERO;
        //砌体房屋占比
        BigDecimal masonryHouseRatio = BigDecimal.ZERO;


        concreteHouseRatio = new BigDecimal(concreteValue).divide(new BigDecimal("100"), 2, BigDecimal.ROUND_HALF_UP);
        masonryHouseRatio = new BigDecimal(masonryValue).divide(new BigDecimal("100"), 2, BigDecimal.ROUND_HALF_UP);


        for (SurTaskManage item : surTaskManageList) {
            String xstmId = item.getXstmId();
            //钢筋混凝土建筑面积
            BigDecimal sumArea1 = BigDecimal.ZERO;
            //砌体建筑面积III-1
            BigDecimal sumArea2 = BigDecimal.ZERO;
            //砌体建筑面积III-2
            BigDecimal sumArea3 = BigDecimal.ZERO;
            HouseCategoryArea typeTotalArea = houseCategoryAreaRepository.getTypeTotalAreaByXstmId(xstmId);
            sumArea1 = sumArea1.add(typeTotalArea.getV2Area().multiply(concreteHouseRatio));
            sumArea2 = sumArea2.add(typeTotalArea.getV3Area().multiply(masonryHouseRatio));
            sumArea3 = sumArea3.add(typeTotalArea.getV4Area().multiply(masonryHouseRatio));
            MhFinishHouseArea mhFinishHouseArea = new MhFinishHouseArea();
            mhFinishHouseArea.setXhfmcrId(UUIDGenerator.getUUID());
            mhFinishHouseArea.setXsemId(xsemId);
            mhFinishHouseArea.setXstmId(xstmId);
            mhFinishHouseArea.setXstmName(item.getEvaluationSubareaName());
            mhFinishHouseArea.setTotalArea(sumArea1.add(sumArea2).add(sumArea3));
            mhFinishHouseArea.setCreateUser(PlatformSessionUtils.getUserId());
            mhFinishHouseArea.setCreateTime(PlatformDateUtils.getCurrentDate());
            mhFinishHouseArea.setConcreteHouseArea(sumArea1);
            mhFinishHouseArea.setMasonryHouseArea1(sumArea2);
            mhFinishHouseArea.setMasonryHouseArea2(sumArea3);
            mhFinishHouseArea.setRatioType(ratioType);
            list.add(mhFinishHouseArea);
        }
        houseFinishLossAssessRepository.batchSave(list);
    }


    @Override
    public List<MhFinishHouseArea> getMhFinishHouseByXsemId(String xsemId) {
        List<MhFinishHouseArea> mhFinishHouseAreaList = houseFinishLossAssessRepository.getMhFinishHouseByXsemId(xsemId);
        MhFinishHouseArea mhFinishHouseArea = new MhFinishHouseArea();
        mhFinishHouseArea.setTotalArea(mhFinishHouseAreaList.stream().map(MhFinishHouseArea::getTotalArea).reduce(BigDecimal.ZERO, BigDecimal::add));
        mhFinishHouseArea.setConcreteHouseArea(mhFinishHouseAreaList.stream().map(MhFinishHouseArea::getConcreteHouseArea).reduce(BigDecimal.ZERO, BigDecimal::add));
        mhFinishHouseArea.setMasonryHouseArea1(mhFinishHouseAreaList.stream().map(MhFinishHouseArea::getMasonryHouseArea1).reduce(BigDecimal.ZERO, BigDecimal::add));
        mhFinishHouseArea.setMasonryHouseArea2(mhFinishHouseAreaList.stream().map(MhFinishHouseArea::getMasonryHouseArea2).reduce(BigDecimal.ZERO, BigDecimal::add));
        mhFinishHouseArea.setXstmName("灾区合计");
        mhFinishHouseAreaList.add(mhFinishHouseArea);
        return mhFinishHouseAreaList;
    }

    @Override
    public HouseFinishDamageLossRatio getHouseFinishDlRatioByXsemId(String xsemId) {
        return houseFinishDamageLossRatioRepository.getHouseFinishDlRatioByXsemId(xsemId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void houseFinishDlRatioUpdate(HouseFinishDamageLossRatio entity) {
        entity.setUpdateUser(PlatformSessionUtils.getUserId());
        entity.setUpdateTime(PlatformDateUtils.getCurrentDate());
        houseFinishDamageLossRatioRepository.houseFinishDlRatioUpdate(entity);
    }

    @Override
    public CorrectionFactor getCorrectionFactorByXsemId(String xsemId) {
        return correctionFactorRepository.getCorrectionFactorByXsemId(xsemId);
    }

    @Override
    public CorrectionFactor updateCorrectionFactor(CorrectionFactor entity) {
        entity.setUpdateUser(PlatformSessionUtils.getUserId());
        entity.setUpdateTime(PlatformDateUtils.getCurrentDate());
        correctionFactorRepository.updateCorrectionFactor(entity);
        return entity;
    }

    @Override
    public void saveCorrectionFactor(CorrectionFactor entity) {
        entity.setXcfId(UUIDGenerator.getUUID());
        entity.setCreateUser(PlatformSessionUtils.getUserId());
        entity.setCreateTime(PlatformDateUtils.getCurrentDate());
        entity.setDelFlag("0");
        correctionFactorRepository.saveCorrectionFactor(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void disasterAreaHouseFinishEconomicLossCount(Map<String, Object> map) {
        String xsemId = (String) map.get("xsemId");
        String economicCoefficient = (String) map.get("economicCoefficient");
        String usageCoefficient = (String) map.get("usageCoefficient");
        houseFinishEconomicLossRepository.deleteByXsemId(xsemId, "1");
        List<HouseFinishEconomicLoss> list = houseFinishEconomicLossRepository.getSubZoneTotalLossByXsemId(xsemId);
        for (HouseFinishEconomicLoss item : list) {
            item.setXhfelId(UUIDGenerator.getUUID());
            item.setXsemId(xsemId);
            item.setCreateTime(new Date());
            item.setCreateUser(PlatformSessionContext.getUserID());
            item.setTotalLoss(item.getTotalLoss().multiply(new BigDecimal(economicCoefficient)).multiply(new BigDecimal(usageCoefficient)));
            item.setType("1");
        }
        HouseFinishEconomicLoss houseFinishEconomicLoss = new HouseFinishEconomicLoss();
        houseFinishEconomicLoss.setXstmName("灾区合计");
        houseFinishEconomicLoss.setTotalLoss(list.stream().map(HouseFinishEconomicLoss::getTotalLoss).reduce(BigDecimal.ZERO, BigDecimal::add));
        houseFinishEconomicLoss.setType("1");
        houseFinishEconomicLoss.setXhfelId(UUIDGenerator.getUUID());
        houseFinishEconomicLoss.setXsemId(xsemId);
        houseFinishEconomicLoss.setCreateTime(new Date());
        houseFinishEconomicLoss.setCreateUser(PlatformSessionContext.getUserID());
        list.add(houseFinishEconomicLoss);
        houseFinishEconomicLossRepository.batchSave(list);
    }

    private BigDecimal percentageConvert(BigDecimal percentage) {
        return percentage.divide(new BigDecimal("100"), 2, BigDecimal.ROUND_HALF_UP);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void houseFinishEconomicLossCount(Map<String, Object> map) {
        List<HouseFinishEconomicLoss> list = new ArrayList<>();
        String xsemId = (String) map.get("xsemId");
        String ratioType = (String) map.get("ratioType");
        //删除上一次计算结果
        houseFinishEconomicLossRepository.deleteByXsemId(xsemId, "0");

        BigDecimal concreteHouseRatio = new BigDecimal((String) map.get("concreteValue")).divide(new BigDecimal("100"), 2, BigDecimal.ROUND_HALF_UP);
        BigDecimal masonryHouseRatio = new BigDecimal((String) map.get("masonryValue")).divide(new BigDecimal("100"), 2, BigDecimal.ROUND_HALF_UP);

        //获取当前地震事件下数据齐全的评估子区
        List<SurTaskManage> surTaskManageList = houseEconomicLossAssessService.getEvaluationSubArea(xsemId);
        List<SurTaskManage> filteredList = surTaskManageList.stream()
                .filter(entity -> entity.getEvaluationSubareaType().equals("城市评估区"))
                .collect(Collectors.toList());
        for (SurTaskManage item : filteredList) {
            //Sd 总面积
            BigDecimal totalArea = BigDecimal.ZERO;
            //Pd 单价
            BigDecimal price = BigDecimal.ZERO;
            //Dd 损失比
            BigDecimal intactLossRatio = BigDecimal.ZERO; //基本完好
            BigDecimal minorLossRatio = BigDecimal.ZERO; //轻微破坏
            BigDecimal moderateLossRatio = BigDecimal.ZERO;//中等破坏
            BigDecimal seriousLossRatio = BigDecimal.ZERO; //严重破坏
            BigDecimal destroyLossRatio = BigDecimal.ZERO; //毁坏
            //Rd 破坏比
            BigDecimal intactFailureRatio = BigDecimal.ZERO; //基本完好
            BigDecimal minorFailureRatio = BigDecimal.ZERO; //轻微破坏
            BigDecimal moderateFailureRatio = BigDecimal.ZERO;//中等破坏
            BigDecimal seriousFailureRatio = BigDecimal.ZERO; //严重破坏
            BigDecimal destroyFailureRatio = BigDecimal.ZERO; //毁坏
            String xstmId = item.getXstmId();
            //Sd——该评估子区同类中高档装修房屋总建筑面积
            MhFinishHouseArea mhFinishHouseArea = houseFinishLossAssessRepository.findByXstmId(xstmId);
            if (PlatformObjectUtils.isEmpty(mhFinishHouseArea)) {
                throw new RuntimeException("计算失败,请先维护房屋装修损失评估中的中高档房屋装修总面积数据");
            }
            //Pd——该评估子区同类房屋中高档装修的重置单价
            List<ResetPrice> resetPriceList = resetPriceRepository.getResetPriceByXsemId(xsemId);
            if (resetPriceList.size() < 1) {
                throw new RuntimeException("计算失败,请先维护地震灾害直接损失评估中的重置单价数据");
            }
            //Dd——该评估子区同类房屋某种破坏等级的装修破坏损失比
            HouseFinishDamageLossRatio houseFinishDlRatio = this.getHouseFinishDlRatioByXsemId(xsemId);
            if (PlatformObjectUtils.isEmpty(houseFinishDlRatio)) {
                throw new RuntimeException("计算失败,请先维护房屋装修损失评估中的房屋装修破坏损失比数据");
            }
            //Rd——该评估子区同类房屋某种破坏等级的破坏比
            List<HouseFailureRatioEntity> houseFailureRatioList = houseEconomicLossAssessRepository.getHouseFailureRatioByXstmId(xstmId);
            if (houseFailureRatioList.size() < 1) {
                throw new RuntimeException("计算失败,请先维护房屋直接经济损失评估中的房屋破坏比数据");
            }
            for (ResetPrice resetPrice : resetPriceList) {

                HouseFinishEconomicLoss houseFinishEconomicLoss = new HouseFinishEconomicLoss();
                houseFinishEconomicLoss.setXhfelId(UUIDGenerator.getUUID());
                houseFinishEconomicLoss.setCreateUser(PlatformSessionContext.getUserID());
                houseFinishEconomicLoss.setCreateTime(new Date());
                houseFinishEconomicLoss.setXsemId(xsemId);
                houseFinishEconomicLoss.setXstmId(xstmId);
                houseFinishEconomicLoss.setXstmName(item.getEvaluationSubareaName());
                houseFinishEconomicLoss.setRatioType(ratioType);


                Optional<HouseFailureRatioEntity> resultEntity = houseFailureRatioList.stream()
                        .filter(entity -> entity.getStructureType().equals(resetPrice.getStructureType())) // 这里的0是你要筛选的type字段的值
                        .findFirst();
                HouseFailureRatioEntity entity = resultEntity.orElseThrow(NoSuchElementException::new);

                intactFailureRatio = new BigDecimal(entity.getIntactRatio());
                minorFailureRatio = new BigDecimal(entity.getMinorRatio());
                moderateFailureRatio = new BigDecimal(entity.getModerateRatio());
                seriousFailureRatio = new BigDecimal(entity.getSeriousRatio());
                destroyFailureRatio = new BigDecimal(entity.getDestroyRatio());

                if (XczhConstants.TWO_TYPE.equals(resetPrice.getStructureType())) {
                    totalArea = mhFinishHouseArea.getConcreteHouseArea();
                    price = concreteHouseRatio.multiply(new BigDecimal(resetPrice.getResetPrice()));
                    intactLossRatio = percentageConvert(houseFinishDlRatio.getLossRatio1());
                    minorLossRatio = percentageConvert(houseFinishDlRatio.getLossRatio2());
                    moderateLossRatio = percentageConvert(houseFinishDlRatio.getLossRatio3());
                    seriousLossRatio = percentageConvert(houseFinishDlRatio.getLossRatio4());
                    destroyLossRatio = percentageConvert(houseFinishDlRatio.getLossRatio5());


                    houseFinishEconomicLoss.setType("0");
                    houseFinishEconomicLoss.setStructureType("II类(钢筋混凝土房屋)");
                    houseFinishEconomicLoss.setBasicallyIntactLoss(resultCount(totalArea, price, intactLossRatio, intactFailureRatio));
                    houseFinishEconomicLoss.setMinorDamageLoss(resultCount(totalArea, price, minorLossRatio, minorFailureRatio));
                    houseFinishEconomicLoss.setModerateDamageLoss(resultCount(totalArea, price, moderateLossRatio, moderateFailureRatio));
                    houseFinishEconomicLoss.setSeriousDamageLoss(resultCount(totalArea, price, seriousLossRatio, seriousFailureRatio));
                    houseFinishEconomicLoss.setDestroyLoss(resultCount(totalArea, price, destroyLossRatio, destroyFailureRatio));
                    houseFinishEconomicLoss.setTotalLoss(houseFinishEconomicLoss.getBasicallyIntactLoss().add(houseFinishEconomicLoss.getMinorDamageLoss()).
                            add(houseFinishEconomicLoss.getModerateDamageLoss()).add(houseFinishEconomicLoss.getSeriousDamageLoss()).add(houseFinishEconomicLoss.getDestroyLoss()));
                    list.add(houseFinishEconomicLoss);
                } else if (XczhConstants.THREE_ONE_TYPE.equals(resetPrice.getStructureType())) {
                    totalArea = mhFinishHouseArea.getMasonryHouseArea1();
                    price = masonryHouseRatio.multiply(new BigDecimal(resetPrice.getResetPrice()));
                    intactLossRatio = percentageConvert(houseFinishDlRatio.getLossRatio6());
                    minorLossRatio = percentageConvert(houseFinishDlRatio.getLossRatio7());
                    moderateLossRatio = percentageConvert(houseFinishDlRatio.getLossRatio8());
                    seriousLossRatio = percentageConvert(houseFinishDlRatio.getLossRatio9());
                    destroyLossRatio = percentageConvert(houseFinishDlRatio.getLossRatio10());


                    for (HouseFailureRatioEntity houseFailureRatio : houseFailureRatioList) {
                        if (resetPrice.getStructureType().equals(houseFailureRatio.getStructureType())) {
                            intactFailureRatio = new BigDecimal(houseFailureRatio.getIntactRatio());
                            minorFailureRatio = new BigDecimal(houseFailureRatio.getMinorRatio());
                            moderateFailureRatio = new BigDecimal(houseFailureRatio.getModerateRatio());
                            seriousFailureRatio = new BigDecimal(houseFailureRatio.getSeriousRatio());
                            destroyFailureRatio = new BigDecimal(houseFailureRatio.getDestroyRatio());
                        }
                        break;
                    }

                    houseFinishEconomicLoss.setType("0");
                    houseFinishEconomicLoss.setStructureType("III-1类(砌体房屋)");
                    houseFinishEconomicLoss.setBasicallyIntactLoss(resultCount(totalArea, price, intactLossRatio, intactFailureRatio));
                    houseFinishEconomicLoss.setMinorDamageLoss(resultCount(totalArea, price, minorLossRatio, minorFailureRatio));
                    houseFinishEconomicLoss.setModerateDamageLoss(resultCount(totalArea, price, moderateLossRatio, moderateFailureRatio));
                    houseFinishEconomicLoss.setSeriousDamageLoss(resultCount(totalArea, price, seriousLossRatio, seriousFailureRatio));
                    houseFinishEconomicLoss.setDestroyLoss(resultCount(totalArea, price, destroyLossRatio, destroyFailureRatio));
                    houseFinishEconomicLoss.setTotalLoss(houseFinishEconomicLoss.getBasicallyIntactLoss().add(houseFinishEconomicLoss.getMinorDamageLoss()).
                            add(houseFinishEconomicLoss.getModerateDamageLoss()).add(houseFinishEconomicLoss.getSeriousDamageLoss()).add(houseFinishEconomicLoss.getDestroyLoss()));
                    list.add(houseFinishEconomicLoss);
                } else if (XczhConstants.THREE_TWO_TYPE.equals(resetPrice.getStructureType())) {
                    totalArea = mhFinishHouseArea.getMasonryHouseArea2();
                    price = masonryHouseRatio.multiply(new BigDecimal(resetPrice.getResetPrice()));
                    intactLossRatio = percentageConvert(houseFinishDlRatio.getLossRatio6());
                    minorLossRatio = percentageConvert(houseFinishDlRatio.getLossRatio7());
                    moderateLossRatio = percentageConvert(houseFinishDlRatio.getLossRatio8());
                    seriousLossRatio = percentageConvert(houseFinishDlRatio.getLossRatio9());
                    destroyLossRatio = percentageConvert(houseFinishDlRatio.getLossRatio10());


                    for (HouseFailureRatioEntity houseFailureRatio : houseFailureRatioList) {
                        if (resetPrice.getStructureType().equals(houseFailureRatio.getStructureType())) {
                            intactFailureRatio = new BigDecimal(houseFailureRatio.getIntactRatio());
                            minorFailureRatio = new BigDecimal(houseFailureRatio.getMinorRatio());
                            moderateFailureRatio = new BigDecimal(houseFailureRatio.getModerateRatio());
                            seriousFailureRatio = new BigDecimal(houseFailureRatio.getSeriousRatio());
                            destroyFailureRatio = new BigDecimal(houseFailureRatio.getDestroyRatio());
                        }
                        break;
                    }

                    houseFinishEconomicLoss.setType("0");
                    houseFinishEconomicLoss.setStructureType("III-2类(砌体房屋)");
                    houseFinishEconomicLoss.setBasicallyIntactLoss(resultCount(totalArea, price, intactLossRatio, intactFailureRatio));
                    houseFinishEconomicLoss.setMinorDamageLoss(resultCount(totalArea, price, minorLossRatio, minorFailureRatio));
                    houseFinishEconomicLoss.setModerateDamageLoss(resultCount(totalArea, price, moderateLossRatio, moderateFailureRatio));
                    houseFinishEconomicLoss.setSeriousDamageLoss(resultCount(totalArea, price, seriousLossRatio, seriousFailureRatio));
                    houseFinishEconomicLoss.setDestroyLoss(resultCount(totalArea, price, destroyLossRatio, destroyFailureRatio));
                    houseFinishEconomicLoss.setTotalLoss(houseFinishEconomicLoss.getBasicallyIntactLoss().add(houseFinishEconomicLoss.getMinorDamageLoss()).
                            add(houseFinishEconomicLoss.getModerateDamageLoss()).add(houseFinishEconomicLoss.getSeriousDamageLoss()).add(houseFinishEconomicLoss.getDestroyLoss()));
                    list.add(houseFinishEconomicLoss);
                }
            }

        }
        houseFinishEconomicLossRepository.batchSave(list);
    }

    private BigDecimal resultCount(BigDecimal area, BigDecimal price, BigDecimal lossRatio, BigDecimal failureRatio) {

        return area.multiply(price).multiply(lossRatio).multiply(failureRatio).divide(new BigDecimal("10000"));
    }

    @Override
    public List<HouseFinishEconomicLoss> getHouseFinishEconomicLossByXsemId(String xsemId, String type) {
        List<HouseFinishEconomicLoss> list = houseFinishEconomicLossRepository.getLossInfo(xsemId, type);
        return list;
    }

    @Override
    public void middleHighRenovationRatioSave(MiddleHighRenovationRatio middleHighRenovationRatio) {
        String userID = PlatformSessionContext.getUserID();
        MiddleHighRenovationRatio findByXsemId = middleHighRenovationRatioRepository.findByXsemId(middleHighRenovationRatio.getXsemId(), middleHighRenovationRatio.getType());
        if (PlatformObjectUtils.isEmpty(findByXsemId)) {
            //保存
            middleHighRenovationRatio.setId(UUIDGenerator.getUUID());
            middleHighRenovationRatio.setCreateTime(new Date());
            middleHighRenovationRatio.setCreateUser(userID);
            middleHighRenovationRatioRepository.save(middleHighRenovationRatio);
        } else {
            //修改
            middleHighRenovationRatio.setUpdateTime(new Date());
            middleHighRenovationRatio.setUpdateUser(userID);
            middleHighRenovationRatioRepository.update(middleHighRenovationRatio);
        }
    }

    @Override
    public MiddleHighRenovationRatio middleHighRenovationRatioView(String xsemId, String type) {
        return middleHighRenovationRatioRepository.findByXsemId(xsemId, type);
    }

    @Override
    public void houseFinishDlRatioSave(HouseFinishDamageLossRatio entity) {
        entity.setXhfdlrId(UUIDGenerator.getUUID());
        entity.setCreateUser(PlatformSessionUtils.getUserId());
        entity.setCreateTime(PlatformDateUtils.getCurrentDate());
        entity.setDelFlag("0");
        houseFinishDamageLossRatioRepository.houseFinishDlRatioSave(entity);
    }
}
